<?php

namespace addons\sum\job;

use addons\oil\model\Order;
use addons\oil\model\OrderAction;
use addons\sum\model\Config;
use think\queue\Job;


/**
 * 订单自动操作
 */
class OrderAutoOper extends BaseJob
{
    /**
     * 转赠的订单到期未领自动返回
     */
    public function autoBack(Job $job, $data){
        try {
            $order = $data['order'];
            // 重新查询订单
            $order = Order::get($order['id']);
            //有订单。订单是已转赠。订单未被领取
            if ($order && $order['status'] == 3 && $order['is_received'] == 0) {
                \think\Db::transaction(function () use ($order, $data) {
                    // 执行关闭返还
                    $order->status = Order::STATUS_PAYED;
                    $order->ext = json_encode($order->setExt($order, ['back_time' => time()]));      // 返还时间
                    $order->save();
                    OrderAction::operAdd($order, null, 'system', '订单转赠未领取到期自动返还');
                });
            }
            // 删除 job
            $job->delete();
        } catch (\Exception $e) {
            // 队列执行失败
            \think\Log::write('queue-' . get_class() . '-autoBack' . '：执行失败，错误信息：' . $e->getMessage());
        }
    }
}